package twentytwenty;

/**
 * @Author: Spider Man
 * @Description: 28.实现strStr函数
 * @Date Created in 2020-09-22 12:32
 * @Modified By:
 */
public class strStr_28 {
    public static void main(String[] args) {
        String haystack = "abaaa", needle = "ba";
        System.out.println(strStr(haystack, needle));
    }

    public static int strStr(String haystack, String needle) {
        if (needle.equals("")) return 0;
        if (needle.length() > haystack.length()) return -1;
        for (int i = 0; i < haystack.length(); i++) {
            if (i + needle.length() > haystack.length() ){
                return -1;
            }else if (haystack.substring(i, i + needle.length()).equals(needle)) {
                return i;
            }
        }
        return -1;
    }
}
